
// A lightweight FastMcpServer subclass that mimics Python FastMCP syntax
// while wrapping JS SDK's lower-level API.

import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { z } from "zod";

// Utility: wrap return value into MCP content
function wrapContent(result) {
  if (result === undefined || result === null) {
    return [{ type: "text", text: "null" }];
  }
  if (typeof result === "string") {
    return [{ type: "text", text: result }];
  }
  return [{ type: "text", text: String(result) }];
}

export class FastMcpServer extends McpServer {
  constructor(options = {}) {
    super({ name: options.name ?? "FastMCP-JS", version: options.version ?? "1.0.0" });
  }

  // Python-like simple tool API: mcp.tool("add", ({a,b}) => a+b)
  tool(name, description = "", schema = null, fn=null) {
    // If schema not given, default to "any object"
    const finalSchema = schema ?? z.record(z.any());

    super.tool(
      name,
      description,
      finalSchema,
      async (args) => {
        const result = await fn(args);
        return { content: wrapContent(result) };
      }
    );
  }
}

